package hl.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import hl.dao.BaseDao;
import hl.model.TVenueInfo;
import hl.pageModel.DataGrid;
import hl.pageModel.VenueInfo;
import hl.service.VenueInfoService;

@Service("venueInfoService")
public class VenueInfoServiceImpl implements VenueInfoService {
	
	private BaseDao<TVenueInfo> venueInfoDao;

	public BaseDao<TVenueInfo> getVenueInfoDao() {
		return venueInfoDao;
	}
	
	@Autowired
	public void setVenueInfoDao(BaseDao<TVenueInfo> venueInfoDao) {
		this.venueInfoDao = venueInfoDao;
	}
	


	@Override
	public DataGrid getDatagrid(VenueInfo venueInfo) {
		DataGrid dg = new DataGrid();
		String hql = "from TVenueInfo t ";

		Map<String, Object> params = new HashMap<String, Object>();
//		if (menu.getText() != null && !menu.getText().trim().equals("")) {
//			hql += " where t.text like :text ";
//			params.put("text", "%" + menu.getText().trim() + "%");
//		}
		
		if (venueInfo.getOrder() != null) {
			hql += " order by " + venueInfo.getSort() + " " + venueInfo.getOrder();
		}

		List<TVenueInfo> l = venueInfoDao.find(hql, params, venueInfo.getPage(), venueInfo.getRows());

		int totalHql = venueInfoDao.count(hql, params);

		List<VenueInfo> pl = new ArrayList();
		if (l != null && l.size() > 0) {
			for (TVenueInfo t : l) {
				VenueInfo u = new VenueInfo();
				BeanUtils.copyProperties(t, u);
				pl.add(u);
			}
		}
		dg.setRows(pl);
		dg.setTotal(totalHql);
		return dg;
	}
	
	
	@Override
	public void save(VenueInfo venueInfo) {
		TVenueInfo t = new TVenueInfo();
		BeanUtils.copyProperties(venueInfo, t);
		t.setCdatetime(new Date());
		venueInfoDao.save(t);
	}
	
	@Override
	public void edit(VenueInfo venueInfo) {
		TVenueInfo t = new TVenueInfo();
		BeanUtils.copyProperties(venueInfo, t);
		t.setMdatetime(new Date());
		venueInfoDao.saveOrUpdate(t);
	}

	@Override
	public void remove(VenueInfo venueInfo) {
		TVenueInfo t =venueInfoDao.get(TVenueInfo.class, venueInfo.getPkVenueInfo());
		venueInfoDao.delete(t);
	}

	@Override
	public Object getComboBox() {
		List<TVenueInfo> l=venueInfoDao.find("from TVenueInfo");
		List<VenueInfo> pl = new ArrayList();
		if (l != null && l.size() > 0) {
			for (TVenueInfo t : l) {
				VenueInfo u = new VenueInfo();
				BeanUtils.copyProperties(t, u);
				pl.add(u);
			}
		}
		return pl;
	}
}
